package cn.bytecollege.user.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.Address;
import cn.bytecollege.common.entity.User;
import cn.bytecollege.common.util.DBHelper;

import java.util.List;

public class AddressDao extends BaseDao<Address,Integer> {
    DBHelper dbHelper=null;
    public AddressDao(){
        dbHelper=new DBHelper();
    }
    /**
     * 增加地址
     */
    @Override
    public int save(Address address) {
        String sql="INSERT INTO `transaction`.`address`( `address_consignee`, " +
                " `user_id`, `address_profile`, `address_province`, `address_city`, `address_county`,  " +
                "`address_phonenumber`) VALUES (?,?,?,?,?,?,?);";
        Object[] objects={address.getAddressConsignee(),address.getUserId(),address.getAddressProfile(),
            address.getAddressProvince(),address.getAddressCity(),address.getAddressCounty(),address.getAddressPhonenumber()};
       return dbHelper.executeUpdate(sql,objects);
    }
    /**
     * 根据用户姓名查找用户Id
     */
    public User findByname(String name){
        String sql="select user_id ,user_name,user_password,user_phonenumber from user where user_name=?";
        List<User> lists= dbHelper.executeQuery(sql,User.class,name);
        User user=null;
        if(lists.size()>0){
             user=lists.get(0);
        }
        return user;
    }
    /**
     * 根据用户Id查询出其创建的所有地址
     */
    public List<Address> findlistById(Integer ID){
        String sql= "select address_id,address_consignee,address_profile,address_province,address_city " +
                " ,address_county,address_phonenumber from address where user_id=?;";
        return dbHelper.executeQuery(sql,Address.class,ID);
    }
    /**
     * 根据地址Id查询出当前地址信息
     */
    @Override
    public Address findById(Integer ID) {
        String sql="select address_id,address_consignee,address_profile,address_province,address_city " +
                " ,address_county,address_phonenumber from address where address_id=?";
         List<Address> lists=dbHelper.executeQuery(sql,Address.class,ID);
         Address address=null;
         if(lists.size()>0){
             address=lists.get(0);
         }
         return address;

    }

    /**
     * 根据提交地址进行修改
     * @param address
     * @return
     */
    @Override
    public int update(Address address) {
        String sql="UPDATE `transaction`.`address` SET `address_consignee` = ?, " +
                " `address_profile` = ?, `address_province` = ?, `address_city` = ?, " +
                "`address_county` = ?, `address_phonenumber` = ? WHERE `address_id` = ?;";
        Object[] objects={address.getAddressConsignee(),address.getAddressProfile(),address.getAddressProvince()
        ,address.getAddressCity(),address.getAddressCounty(),address.getAddressPhonenumber(),address.getAddressId()};
        return  dbHelper.executeUpdate(sql,objects);
    }

    /**
     * 根据地址ID删除地址
     */
    @Override
    public int deleteById(Integer Id) {
       String sql ="DELETE from address where address_id=?";
       return dbHelper.executeUpdate(sql,Id);
    }
}
